<?php

namespace App\Service\Api;

use App\Models\Wanggui\WangguiDotYhatByMercatorModel;
use App\Models\Wanggui\WangguiDotYhatModel;
use App\Service\BaseService;
use App\Service\Common\CoordinateService;
use Carbon\Carbon;
use Illuminate\Support\Facades\Artisan;

class WangguiApiService extends BaseService
{

    /**
     * 导入预测数据
     * @param string $fileUrl
     * @param string $type
     * @return void
     */
    public static function importWgPrediction(string $fileUrl, $type = 'default')
    {
        ob_start();
        switch ($type){
            case 'default':
                Artisan::call("siteLocation:import-wg-prediction {$fileUrl}");
                break;
            case 'mercator':
                Artisan::call("siteLocation:import-wg-prediction-by-mercator {$fileUrl}");
                break;
            default:
        }



        ob_clean();
    }

    /**
     * 点位预测
     * @param string $wgNumber
     * @return array
     */
    public static function getPredictionByWgNumber(string $wgNumber): array
    {
        $data = [];
        WangguiDotYhatModel::query()
            ->where('wgNumber', $wgNumber)
            ->whereBetween('date', [Carbon::now()->startOfDay()->timestamp, Carbon::now()->addDays(29)->timestamp])
            ->get()
            ->each(function (WangguiDotYhatModel $wangguiDotYhatModel) use (&$data) {
                $data[Carbon::createFromTimestamp($wangguiDotYhatModel->date)->toDateString()] = [
                    'delivery' => [
                        "yhat"       => $wangguiDotYhatModel['delivery_yhat'],
                        "yhat_lower" => $wangguiDotYhatModel['delivery_yhat_lower'],
                        "yhat_upper" => $wangguiDotYhatModel['delivery_yhat_upper']
                    ],
                    'dine_in'     => [
                        "yhat"       => $wangguiDotYhatModel['dine_in_yhat'],
                        "yhat_lower" => $wangguiDotYhatModel['dine_in_yhat_lower'],
                        "yhat_upper" => $wangguiDotYhatModel['dine_in_yhat_upper']
                    ],
                    'total'    => [
                        "yhat"       => $wangguiDotYhatModel['total_yhat'],
                        "yhat_lower" => $wangguiDotYhatModel['total_yhat_lower'],
                        "yhat_upper" => $wangguiDotYhatModel['total_yhat_upper']
                    ]
                ];
            });
        return $data;
    }

    /**
     * 点位预测
     * @param string $wgNumber
     * @return array
     */
    public static function getHistoricalPredictionByWgNumber(string $wgNumber): array
    {
        return WangguiDotYhatModel::query()
            ->where('wgNumber', $wgNumber)
            ->whereBetween('date', [Carbon::now()->subDays(31)->timestamp, Carbon::now()->subDay()->timestamp])
            ->get()
            ->map(function (WangguiDotYhatModel $wangguiDotYhatModel) {
                return [
                    'time' => Carbon::createFromTimestamp($wangguiDotYhatModel->date)->format('m/d'),
                    'num'  => (int)($wangguiDotYhatModel['total_yhat'] * 100)
                ];
            })->toArray();
    }

    public static function getPredictionByCoordinate($lng, $lat): array
    {
        // 根据经纬度获得Mercator坐标
        $bd09   = CoordinateService::gcj02Tobd09($lng, $lat);
        $bd09mc = CoordinateService::bd09ToMercator($bd09['lon'], $bd09['lat']);

        $d = 1000;

        $x = floor($bd09mc[0] / $d);
        $y = floor($bd09mc[1] / $d);

        // 判断x和y的范围
        if ($y < 4754 || $y > 4992 || $x < 12847 || $x > 12964) {
            self::throwErr('超出规定值', 422);
        }

        $data = [];
        WangguiDotYhatByMercatorModel::query()
            ->where('mercatorId', $y . '@' . $x)
            ->where('date', '>', Carbon::now()->timestamp)
            ->get()
            ->each(function (WangguiDotYhatByMercatorModel $wangguiDotYhatByMercatorModel) use (&$data) {
                $data[Carbon::createFromTimestamp($wangguiDotYhatByMercatorModel->date)->toDateString()] = [
                    'delivery' => [
                        "yhat"       => $wangguiDotYhatByMercatorModel['delivery_yhat'],
                        "yhat_lower" => $wangguiDotYhatByMercatorModel['delivery_yhat_lower'],
                        "yhat_upper" => $wangguiDotYhatByMercatorModel['delivery_yhat_upper']
                    ],
                    'dine_in'     => [
                        "yhat"       => $wangguiDotYhatByMercatorModel['dine_in_yhat'],
                        "yhat_lower" => $wangguiDotYhatByMercatorModel['dine_in_yhat_lower'],
                        "yhat_upper" => $wangguiDotYhatByMercatorModel['dine_in_yhat_upper']
                    ],
                    'total'    => [
                        "yhat"       => $wangguiDotYhatByMercatorModel['total_yhat'],
                        "yhat_lower" => $wangguiDotYhatByMercatorModel['total_yhat_lower'],
                        "yhat_upper" => $wangguiDotYhatByMercatorModel['total_yhat_upper']
                    ]
                ];
            });
        return $data;


    }
}
